package com.example.healkep.MainActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.healkep.R;
import org.json.JSONArray;
import java.util.Arrays;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


public class MainActivityShowFood extends Activity {

    private int[] day_food = new int[3];

    private String id = com.example.healkep.LoginActivity.getUser();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.activity_show_food );
        final EditText date = (EditText) findViewById ( R.id.date );
        Button button2 = findViewById ( R.id.login );
        Button button1 = findViewById ( R.id.choose );
//绑定监听事件

        button1.setOnClickListener ( new View.OnClickListener () {
            @Override
            public void onClick(View view) {
                new Thread ( new Runnable () {
                    @Override
                    public void run() {
                        try {
                            String finaldate = date.getText ().toString ();
                            System.out.println ( finaldate );
                            final String json ="{\n" +
                                    "    \"user\":\""+id+"\",\n" +
                                    "    \"cdate\":\""+finaldate+"\"\n" +
                                    "}";
                            OkHttpClient client = new OkHttpClient ();  //创建http客户端
                            Request request = new Request.Builder (  )
                                    .url ( com.example.healkep.LoginActivity.url+"/food2/day" )
                                    .post ( RequestBody.create ( MediaType.parse ( "application/json"),json  ) )
                                    .build ();
                            Response response = client.newCall ( request ).execute ();  //执行发送的指令
                            String responseData = response.body ().string ();
                            JSONArray jsonArray = new JSONArray ( responseData );
                            Arrays.fill(day_food, 0);
                            for (int i = 0; i < jsonArray.length (); i++) {
                                if (jsonArray.getJSONObject(i).getInt("time_slot") == 1)
                                    day_food[0]= day_food[0] + (int)(jsonArray.getJSONObject(i).getDouble("cal") );
                                if (jsonArray.getJSONObject(i).getInt("time_slot") == 2)
                                    day_food[1]= day_food[1] + (int)(jsonArray.getJSONObject(i).getDouble("cal") );
                                if (jsonArray.getJSONObject(i).getInt("time_slot") == 3)
                                    day_food[2]= day_food[2] + (int)(jsonArray.getJSONObject(i).getDouble("cal") );

                            }
                            System.out.println ( "day_food: " );
                            System.out.println ( day_food[0] );
                            System.out.println ( day_food[1] );
                            System.out.println ( day_food[2] );
                            runOnUiThread ( new Runnable () {
                                @Override
                                public void run() {
                                    Toast.makeText ( MainActivityShowFood.this, "信息获取成功", Toast.LENGTH_SHORT ).show ();

                                }
                            } );
                        } catch (Exception e) {
                            e.printStackTrace ();
                            runOnUiThread ( new Runnable () {
                                @Override
                                public void run() {
                                    Toast.makeText ( MainActivityShowFood.this, "网络连接失败", Toast.LENGTH_SHORT ).show ();
                                }
                            } );
                        }
                    }
                } ).start ();
            }
        } );

        button2.setOnClickListener ( new View.OnClickListener () {
            @Override
            public void onClick(View view) {
                // 给bnt1添加点击响应事件
                Intent intent2 = new Intent ( MainActivityShowFood.this, MainActivityShowFoodDay.class );
                Bundle bun = new Bundle ();
                bun.putIntArray("day_food", day_food);
                intent2.putExtras ( bun );
                //启动
                startActivity ( intent2 );
            }
        } );

    }
}
